//
//  CZCompanyController.m
//  TCProject
//
//  Created by admin on 16/2/2.
//  Copyright © 2016年 wise-wing. All rights reserved.
//

#import "CZCompanyController.h"
#import "AccountManager.h"
#import "CZCompanyCell.h"

@interface CZCompanyController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) NSArray *companies;
@property (weak, nonatomic) IBOutlet UIView *listContainer;
@property (weak, nonatomic) IBOutlet UITextField *keywordsTextField;

@property (weak, nonatomic) IBOutlet UITableView *tableView;
- (IBAction)okAction:(id)sender;
@property (weak, nonatomic) IBOutlet UIView *detailContainer;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
- (IBAction)searchAction:(id)sender;

@end

@implementation CZCompanyController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView.rowHeight = 44.f;
    
    if (_company) {
        _detailContainer.hidden = NO;
        self.company = _company;
    } else {
        _listContainer.hidden = NO;
        [self getList];
    }

}

- (void)setCompany:(CZCompany *)company
{
    _company = company;
    self.nameLabel.text = company.name;
    self.addressLabel.text = company.addAddress;
}

- (void)getList
{
    BMRequest *request = [BMRequest requestWithPath:@"getAllCompanies.htm" contentKey:@"companies"];
    request.params = @{@"name": _keywordsTextField.text};
    [self showHudWithhint:kDefaultMessage];
    [CZCompany postWithRequest:request finish:^(BMResponse *response, NSError *error) {
        [self hideHud];
        if (response.status == kResultOK) {
            self.companies = response.result;
            [self.tableView reloadData];
        } else {
            [self showHint:@"获取公司信息失败!"];
        }
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _companies.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CZCompanyCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    cell.company = _companies[(NSUInteger) indexPath.row];
    return cell;
}

- (IBAction)searchAction:(id)sender {
    [self getList];
}

- (IBAction)okAction:(id)sender {
    NSIndexPath *indexpath = [self.tableView indexPathForSelectedRow];
    if (!indexpath) {
        [self showHint:@"您还没有选择一个公司!"];
        return;
    }
    CZCompany *company = _companies[(NSUInteger) indexpath.row];
    BMRequest *request = [BMRequest requestWithPath:@"chooseCompany.htm"];
    request.params = @{@"id": @([AccountManager sharedInstance].user.ID), @"company": @(company.ID)};
    [self showHudWithhint:kDefaultMessage];
    [[BMHttpTool sharedInstance] postWith:request finish:^(BMResponse *response, NSError *error) {
        [self hideHud];
        if (response.status == kResultOK) {
            self.listContainer.hidden = YES;
            self.detailContainer.hidden = NO;
            self.company = company;
        } else {
            [self showHint:@"选择公司失败!"];
        }
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%ld",(long)indexPath.row );
}

@end
